﻿// 版权所有：2015  All Rights Reserved.
// 文 件 名：Helper.cs
// 功能描述：
// 创建标识：m.sh.lin0328@163.com 2015/1/25 10:50:32
// 修改描述：
//----------------------------------------------------------------*/
using System;
using System.Collections.Generic;
using System.IO;
using System.Text;
using System.Windows.Media.Imaging;

namespace MslDiary.Framework
{
    public class Helper
    {
        #region 文件格式
        /// <summary>
        /// 
        /// </summary>
        /// <param name="suffix">文件后缀</param>
        /// <returns></returns>
        public static string GetFileType(string suffix)
        {
            suffix = suffix.ToLower();
            if (!suffix.StartsWith(".")) suffix = "." + suffix;
            if (".rar|.zip|.7z|".Contains(suffix + "|")) return "压缩文件|rar";
            if (".txt|".Contains(suffix + "|")) return "文本文本|txt";
            if (".doc|.docx|.xls|.xlsx|.ppt|.pptx|".Contains(suffix + "|")) return "办公文件|doc";
            if (".jpg|.bmp|.png|.gif|".Contains(suffix + "|")) return "图片文件|jpg";
            if (".avi|.mov|.wmv|.3gp|.3gp|.flv|.mpeg|".Contains(suffix + "|")) return "视频文件|avi";
            if (".mp3|.wma|.midi|".Contains(suffix + "|")) return "音频文件|mp3";
            return String.Empty;
        }
        #endregion
        /// <summary>
        /// Image 转换成 byte[]
        /// </summary>
        /// <param name="filePath">图片路径</param>
        /// <returns></returns>
        public static byte[] FileToByte(string filePath)
        {
            byte[] b = null;
            if (!File.Exists(filePath)) return b;
            using (FileStream fs = new FileStream(filePath, FileMode.Open, FileAccess.Read))
            {
                using (BinaryReader br = new BinaryReader(fs))
                {
                    b = br.ReadBytes((int)fs.Length);
                }
            }
            return b;
        }
        private BitmapImage ShowImageFromByte(byte[] img)
        {
            System.IO.MemoryStream ms = new System.IO.MemoryStream(img);//img是从数据库中读取出来的字节数组
            ms.Seek(0, System.IO.SeekOrigin.Begin);

            BitmapImage b = new BitmapImage();
            b.BeginInit();
            b.StreamSource = ms;
            b.EndInit();
            return b;
            //image1.Source = b;
        }

        /// <summary>
        /// 加密 数据库登录密码
        /// </summary>
        /// <param name="account"></param>
        /// <param name="dbLoginPwd"></param>
        /// <returns></returns>
        public static string EncryptDbPwd(string account, string dbLoginPwd)
        {
            if (String.IsNullOrEmpty(account) || String.IsNullOrEmpty(dbLoginPwd))
                return String.Empty;

            Framework.Gloable.ENCRYPY_KEY = account;
            return MSL.Tool.Security.DESEncrypt.Instance.Encrypt(dbLoginPwd, Framework.Gloable.DbEncryptKey);
        }
        /// <summary>
        /// 解密 数据库登录密码
        /// </summary>
        /// <param name="account"></param>
        /// <param name="dbLoginPwd"></param>
        /// <returns></returns>
        public static string DecryptDbPwd(string account, string dbLoginPwd)
        {
            if (String.IsNullOrEmpty(account) || String.IsNullOrEmpty(dbLoginPwd))
                return String.Empty;

            Framework.Gloable.ENCRYPY_KEY = account;
            return MSL.Tool.Security.DESEncrypt.Instance.Decrypt(dbLoginPwd, Framework.Gloable.DbEncryptKey);
        }
    }
}
